package com.yidu.t299.dao.xp;
import com.yidu.t299.domain.xp.Lesson;
import com.yidu.t299.domain.xp.SedulousManage;
import com.yidu.t299.domain.xp.StudentManage;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import java.util.List;

/*
使用Mybatis逆向生成自动生成 后续自己更改
 */
@Mapper
public interface SedulousManageMapper {
    /**
     * 根据出勤编号删除
     * @param sedId
     * @return
     */
    @Delete({
        "delete from sedulous_manage",
        "where sed_id = #{sedId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer sedId);

    /**
     * 增加出勤信息
     * @param record 出勤对象
     * @return 影响的行数(返回1则成功)
     */
    @Insert({
        "insert into sedulous_manage (sed_id, student_id, ",
        "sed_state, lesson_id, ",
        "sed_dates)",
        "values (#{sedId,jdbcType=INTEGER}, #{studentId,jdbcType=INTEGER}, ",
        "#{sedState,jdbcType=VARCHAR}, #{lessonId,jdbcType=INTEGER}, ",
        "#{sedDates,jdbcType=VARCHAR})"
    })
    int insert(SedulousManage record);

    /**
     * 根据出勤编号查询指定信息
     * @param sedId 出勤编号
     * @return
     */
    @Select({
        "select",
        "sed_id, student_id, sed_state, lesson_id, sed_dates",
        "from sedulous_manage",
        "where sed_id = #{sedId,jdbcType=INTEGER}"
    })
    @Results(id = "sedulousManageMap",value = {
        @Result(column="sed_id", property="sedId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="student_id", property="studentId", jdbcType=JdbcType.INTEGER),
        @Result(column="sed_state", property="sedState", jdbcType=JdbcType.VARCHAR),
        @Result(column="lesson_id", property="lessonId", jdbcType=JdbcType.INTEGER),
        @Result(column="sed_dates", property="sedDates", jdbcType=JdbcType.VARCHAR)
    })
    SedulousManage selectByPrimaryKey(Integer sedId);

    /**
     *按条件分页出勤数据集合
     * @param rows 每页的行数
     * @param page 页码
     * @param condition 条件参数
     * @return 出勤对象集合
     */
    @Select("select * from sedulous_manage cm " +
            "join lesson l " +
            "on cm.lesson_id=l.lesson_id " +
            "join student_manage sm " +
            "on cm.student_id=sm.student_id where 1=1 ${condition} limit #{page},#{rows}")
    @Results({
            @Result(column = "sed_id",property = "sedId",jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "student_id",property = "studentId",jdbcType = JdbcType.INTEGER),
            @Result(column = "sed_state",property = "sedState",jdbcType = JdbcType.VARCHAR),
            @Result(column = "lesson_id",property = "lessonId",jdbcType = JdbcType.INTEGER),
            @Result(column = "sed_dates",property = "sedDates",jdbcType = JdbcType.VARCHAR),
            @Result(column = "student_name",property = "studentName",jdbcType = JdbcType.VARCHAR),
            @Result(column = "lesson_name",property = "lessonName",jdbcType = JdbcType.VARCHAR)
    })
    List<SedulousManage> selectByPage(@Param("rows") int rows, @Param("page") int page, @Param("condition") String condition);

    /**
     * 按条件查询sedulous_manage表中数据行数 因为涉及了多表
     * 所以这里也需要多表联查不然无法设置条件
     * @param condition 条件参数
     * @return 数据行数
     */
    @Select("select count(*) from sedulous_manage cm " +
            "join lesson l " +
            "on cm.lesson_id=l.lesson_id " +
            "join student_manage sm " +
            "where 1=1 ${condition}")
    int selectCount(@Param("condition") String condition);

    /**
     * 根据出勤信息修改其数据
     * @param record
     * @return
     */
    @Update({
        "update sedulous_manage",
        "set student_id = #{studentId,jdbcType=INTEGER},",
          "sed_state = #{sedState,jdbcType=VARCHAR},",
          "lesson_id = #{lessonId,jdbcType=INTEGER},",
          "sed_dates = #{sedDates,jdbcType=VARCHAR}",
        "where sed_id = #{sedId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(SedulousManage record);

    //以下是获取下拉框数据的方法
    /**
     * 得到所有学员姓名信息
     * @return 学员名称集合
     */
    @Select("select student_name from student_manage")
    @Results({
            @Result(column = "student_name",property = "studentName")
    })
    List<StudentManage> selectAllGetStudent();

    /**
     * 得到所有课程信息
     * @return 课程集合
     */
    @Select("select lesson_name from lesson")
    @Results({
            @Result(column = "lesson_name",property = "lessonName")
    })
    List<Lesson> selectAllGetlessonName();


    /**
     * 针对出勤表(sedulous_manage)进行操作
     * @param studentName 学员名称
     * @return 影响的行数(>1:成功)
     */
    @Select("select student_id from student_manage where student_name=#{studentName}")
    int selectByStudentName(@Param("studentName") String studentName);

    /**
     * 针对课程表(lesson)进行操作
     * @param lessonName 课程名称
     * @return 影响的行数(>1:成功)
     */
    @Select("select lesson_id from lesson where lesson_name=#{lessonName}")
    int selectByLessonName(@Param("lessonName") String lessonName);
}